﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using SXDXClient;
using System.Threading;
namespace WPLEDClient
{
    public partial class MainPage : PhoneApplicationPage
    {
        LEDClient LEDClient = new LEDClient();
        string IsUseLEDbyIP = "1";
        public string Log
        {
            get
            {
                return txtLog.Text;
            }
            set
            {
                Deployment.Current.Dispatcher.BeginInvoke(() => txtLog.Text = value);
            }
        }
        public string nLEDID
        {
            get
            {
                if (IsUseLEDbyIP == "1")
                {
                    return ((ListBoxItem)(IPAdd.SelectedItem)).Content.ToString().Trim();
                }
                return LEDID.Text.ToString();
            }
        }
        public int nBright
        {
            get
            {
                string value = "";
                value = txtBright.Text.ToString().Trim();
                return System.Convert.ToInt32(value);
            }
        }
        string result = "";
        int x = 0;
        int y = 0;
        int width = 288;
        int height = 64;
        string font = "";
        Int32 inmethod = 1;
        Int32 inspeed = 1;
        Int32 outmethod = 1;
        Int32 outspeed = 1;
        Int32 stopmethod = 0;
        Int32 stopspeed = 1;
        Int32 stoptime = 1000;
        int pwidth = 288;
        int pheight = 64;
        string Font1
        {
            get
            {
                string value = "";
                value = nFont1.Text.ToString().Trim();
                return value;
            }
        }
        string Font2
        {
            get
            {
                string value = "";
                value = nFont2.Text.ToString().Trim();
                return value;
            }
        }
        string Font3
        {
            get
            {
                string value = "";
                value = nFont3.Text.ToString().Trim();
                return value;
            }
        }
        string TXT1
        {
            get
            {
                string value = "";
                value = nText1.Text.ToString().Trim();
                return value;

            }
        }
        string TXT21
        {
            get
            {
                string value = "";
                value = nText21.Text.ToString().Trim();
                return value;

            }
        }
        string TXT22
        {
            get
            {
                string value = "";
                value = nText22.Text.ToString().Trim();
                return value;

            }
        }
        string TXT3
        {
            get
            {
                string value = "";
                value = nText3.Text.ToString().Trim();
                return value;
            }
        }
        int FontS1
        {
            get
            {
                string value = "";
                value = nFontS1.Text.ToString().Trim();
                return System.Convert.ToInt32(value);
            }
        }
        int FontS2
        {
            get
            {
                string value = "";
                value = nFontS2.Text.ToString().Trim();
                return System.Convert.ToInt32(value);
            }
        }
        int FontS3
        {
            get
            {
                string value = "";
                value = nFontS3.Text.ToString().Trim();
                return System.Convert.ToInt32(value);
            }
        }
        public MainPage()
        {
            InitializeComponent();
        }
        private void btn_PowerOn_Click(object sender, RoutedEventArgs e)
        {
            result = "";
            string ledid = nLEDID;
            ThreadPool.QueueUserWorkItem(delegate(object o)
            {
                result = new LEDClient().SwithPower(ledid, 1, 0);
                Log = result;
            });
        }
        private void btn_PowerOff_Click(object sender, RoutedEventArgs e)
        {
            result = "";
            string ledid = nLEDID;
            ThreadPool.QueueUserWorkItem(delegate(object o)
            {
                result = new LEDClient().SwithPower(ledid, 0, 0);
                Log = result;
            });
        }

        private void btn_PowerOnWithIP_Click(object sender, RoutedEventArgs e)
        {
            result = "";
            StartUpAndShowIP(nLEDID);
        }

        private void btn_SetLight_Click(object sender, RoutedEventArgs e)
        {
            string ledid = nLEDID;
            int br = nBright;
            ThreadPool.QueueUserWorkItem(delegate(object o)
            {
                result = new LEDClient().LED_SetBright(ledid, br);
                Log = result;
            });
        }

        private void btn_GetLight_Click(object sender, RoutedEventArgs e)
        {
            string ledid = nLEDID;
            ThreadPool.QueueUserWorkItem(delegate(object o)
            {
                result = new LEDClient().LED_GetBright(ledid);
                Log = result;
            });

        }

        private void btn_AdjustTime_Click(object sender, RoutedEventArgs e)
        {
            string ledid = nLEDID;
            ThreadPool.QueueUserWorkItem(delegate(object o)
            {
                result = new LEDClient().LED_AdjustTime(ledid);
                Log = result;
            });
        }

        private void btn_FlashContent_Click(object sender, RoutedEventArgs e)
        {
            string text = "";
            int K = 0;
            Log = "Start data";
            string ledid = nLEDID;
            string fn1 = Font1;
            string fn2 = Font2;
            string fn3 = Font3;
            int fs1 = FontS1;
            int fs2 = FontS2;
            int fs3 = FontS3;
            string txt1 = TXT1;
            string txt21 = TXT21;
            string txt22 = TXT22;
            string txt3 = TXT3;
            ThreadPool.QueueUserWorkItem(delegate(object o)
            {
                result = LEDClient.LED_MakeRoot(ledid, "ROOT_DOWNLOAD", "COLOR_MODE_DOUBLE", "SURVIVE_ALWAYS");
                if (result == "")
                {
                    return;
                }
                if (result.StartsWith("EX:"))
                {
                    Log = result;
                    return;
                }
                K = System.Convert.ToInt32(result);
                x = 0; y = 0; width = 288; height = 16;
                uint time = 1000;
                LEDClient.LED_AddChapter(ledid, K, 20 * time, "WAIT_CHILD");
                x = 0; y = 0; width = 288; height = 16;
                LEDClient.LED_AddRegion(ledid, K, x, y, width, height, 0);
                LEDClient.LED_AddLeaf(ledid, K, 20 * time, "WAIT_CHILD");
                LEDClient.LED_AddDateTimeEx(ledid, K, x, y, width, height, 1, 0, 0, fn1, fs1, 0xffff, "WFS_NONE", 0, 0, 0, 0, txt1);

                x = 0; y = 16; width = 288; height = 32;
                LEDClient.LED_AddRegion(ledid, K, x, y, width, height, 0);
                LEDClient.LED_AddLeaf(ledid, K, 10 * time, "WAIT_CHILD");
                inmethod = 1;
                inspeed = 1;
                LEDClient.LED_AddText(ledid, K, 0, 0, width, height, 1, 0, txt21, fn2, fs2, 0xff, "WFS_NONE", 1, 0, inmethod, inspeed, outmethod, outspeed, stopmethod, stopspeed, stoptime);
                LEDClient.LED_AddLeaf(ledid, K, 10 * time, "WAIT_CHILD");
                inmethod = 1; inspeed = 1;
                LEDClient.LED_AddText(ledid, K, 0, 0, width, height, 1, 0, txt22, fn2, fs2, 0xff, "WFS_NONE", 1, 0, inmethod, inspeed, outmethod, outspeed, stopmethod, stopspeed, stoptime);

                x = 0; y = 48; width = 288; height = 16;
                LEDClient.LED_AddRegion(ledid, K, x, y, width, height, 0);
                LEDClient.LED_AddLeaf(ledid, K, 10 * time, "WAIT_CHILD");

                inmethod = 2;
                inspeed = 1;
                LEDClient.LED_AddText(ledid, K, 0, 0, width, height, 1, 0, txt3, fn3, fs3, 0xff, "WFS_NONE", 1, 0, inmethod, inspeed, outmethod, outspeed, stopmethod, stopspeed, stoptime);

                Log = "Start Sending data";
                result = LEDClient.LED_SendToScreen(ledid, K, 1);
            });
        }

        private void btn_ReSetContent_Click(object sender, RoutedEventArgs e)
        {
            string ledid = nLEDID;
            ThreadPool.QueueUserWorkItem(delegate(object o)
            {
                result = LEDClient.LED_ResetDisplay(ledid);
                Log = result;
            });
        }
        public void StartUpAndShowIP(string nLEDID)
        {
            string ledid = nLEDID;
            string ip = ((ListBoxItem)(IPAdd.SelectedItem)).Content.ToString().Trim();
            ThreadPool.QueueUserWorkItem(delegate(object o)
            {
                result = LEDClient.LED_MakeRoot(ledid, "ROOT_DOWNLOAD", "COLOR_MODE_DOUBLE", "SURVIVE_ALWAYS");
                if (result == "")
                {
                    return;
                }
                if (result.StartsWith("EX:"))
                {
                    return;
                }
                ushort K = System.Convert.ToUInt16(result);
                LEDClient.LED_AddChapter(ledid, K, 30000, "WAIT_CHILD");
                LEDClient.LED_AddRegion(ledid, K, 0, 0, 128, 32, 0);
                LEDClient.LED_AddLeaf(ledid, K, 10000, "WAIT_CHILD");
                LEDClient.LED_AddText(ledid, K, 0, 0, 128, 64, 1, 0, ip, "宋体", 12, 0xFF00, "WFS_NONE", 1, 0, 0, 1, 1, 1, 0, 1, 3);
                LEDClient.LED_SendToScreen(ledid, K, 1);
            });
        }

        private void btn_NormalContent_Click(object sender, RoutedEventArgs e)
        {
            string ledid = nLEDID;
            string txt3 = TXT3;
            ThreadPool.QueueUserWorkItem(delegate(object o)
            {
                result = LEDClient.LED_MakeRoot(ledid, "ROOT_PLAY", "COLOR_MODE_DOUBLE", "SURVIVE_ALWAYS", "1");
                if (result == "")
                {
                    return;
                }
                if (result.StartsWith("EX:"))
                {
                    return;
                }
                int K = System.Convert.ToInt32(result);
                LEDClient.LED_AddChapter(ledid, K, 5000, "WAIT_CHILD");
                LEDClient.LED_AddRegion(ledid, K, 0, 0, 288, 64, 0);
                LEDClient.LED_AddLeaf(ledid, K, 5000, "WAIT_CHILD");
                LEDClient.LED_AddText(ledid, K, 0, 0, 288, 64, 1, 0, txt3, "宋体", 48, 120, "WFS_NONE", 0, 0, 2, 5, 1, 0, 1, 0, 1);
                LEDClient.LED_SendToScreen(ledid, K, 1);
            });
        }

        private void btn_Urgent_Click(object sender, RoutedEventArgs e)
        {
            string ledid = nLEDID;
            string txt3 = TXT3;
            ThreadPool.QueueUserWorkItem(delegate(object o)
            {
                result = LEDClient.LED_MakeRoot(ledid, "ROOT_PLAY", "COLOR_MODE_DOUBLE", "SURVIVE_ALWAYS", "3");
                if (result == "")
                {
                    return;
                }
                if (result.StartsWith("EX:"))
                {
                    return;
                }
                int K = System.Convert.ToInt32(result);
                LEDClient.LED_AddChapter(ledid, K, 5000, "WAIT_CHILD");
                LEDClient.LED_AddRegion(ledid, K, 0, 0, 288, 64, 0);
                LEDClient.LED_AddLeaf(ledid, K, 5000, "WAIT_CHILD");
                LEDClient.LED_AddText(ledid, K, 0, 0, 288, 64, 1, 0, txt3, "宋体", 48, 0xFF00, "WFS_NONE", 0, 0, 2, 1, 1, 0, 1, 0, 1000);
                LEDClient.LED_SendToScreen(ledid, K, 1);
                Log = "紧急消息已经发布";
            });
        }
    }
}